var box = document.querySelector('.box')
var ol = document.querySelector('.box ol')
var ulis = document.querySelectorAll('.box ul li')
var olis = document.querySelectorAll('.box ol li')
// console.log(box,ulis,olis);
for (let i = 0; i < ulis.length - 1; i++) {
    ulis[i].onmouseover = function () {
        for (let j = 0; j < ulis.length - 1; j++) {
            ulis[j].className = ''
            olis[j].className = ''
            ol.className = ''
        }
        getStyle(box, 'height')
        // console.dir(box);
        ulis[i].className = 'active'
        olis[i].className = 'active'
        ol.className = 'active'
    }
    box.onmouseout = function () {
        ulis[i].className = ''
        olis[i].className = ''
        ol.className = ''
    }
}

// var arr1 = localStorage.getItem('arr1[]')
// console.log(arr1);
var titleB = document.querySelector('.title');
var priceB = document.querySelector('.price');
var categoryB = document.querySelector('.category');
var detail = document.querySelector('.detail');
var img = document.querySelectorAll('#img')
var enname = document.querySelector('.enname')
var goodsimg = document.querySelector('.goodsimg')
var productCode = /productCode=(\w+)/.exec(location.search)
var score = /score=(\w+)/.exec(location.search)
var id = localStorage.getItem('id')
var token = localStorage.getItem('token')
console.log(id, token,score);
productCode = productCode[1]
score = score[1]
console.log(productCode,score);
if (productCode) {
    var xhr = new XMLHttpRequest;
    xhr.open('post', '/ararar')
    xhr.setRequestHeader('content-type', 'application/json;charset=UTF-8')
    xhr.send('{"channelCode":"BROS"}')
    xhr.onload = function () {
        var res = xhr.responseText;
        res = JSON.parse(res)
        console.log(res);
        var { serializableData } = res;
        // console.log(serializableData[0].getAllProductLastCategoryDataList[0]);

        var data = []
        for (var i = 0; i < serializableData.length; i++) {
            // console.log(serializableData[i].getAllProductLastCategoryDataList);
            for (var j = 0; j < serializableData[i].getAllProductLastCategoryDataList.length; j++) {
                console.log(serializableData[i].getAllProductLastCategoryDataList[j].productList);
                data.push(serializableData[i].getAllProductLastCategoryDataList[j].productList)
            }
        }
        //    根据productCode获取当前商品的其他数据
        for (var i = 0; i < data.length; i++) {
            for (var j = 0; j < data[i].length; j++) {
                // console.log(data[i][j]);
                if (data[i][j].productCode === productCode) {
                    var obj = data[i][j]
                    console.log(obj);
                }
            }
        }
        // console.log(obj);
        // console.log(obj.productName, obj.floorPrice,);
        titleB.innerText = obj.productName
        enname.innerText = obj.productNameEn
        priceB.innerText = obj.floorPrice
        categoryB.innerText = obj.seriesCatalogName
        img.forEach(item => {
            item.src = obj.productImageDefault
        })
        console.log(obj.productImages.length);
        var str = ''
        for (var i = 0; i < obj.productImages.length; i++) {
            str += `
                    <li><img src="${obj.productImages[i]}"></li><br>
                 `
        }
        goodsimg.innerHTML = str
        addcart()


    }
} else {
    alert('productCode为空')
    location.href = "list.html"
}

// 加入购物车

function addcart() {
    document.querySelector('.addCart').onclick = function () {
        //判断用户是否登录
        if (id && token) {
            //存储当前数据到本地
            //先获取本地数据，添加数据
            var str = localStorage.getItem('data')
            console.log(str);
            var obj = {
                productCode,
                score
            }
            console.log(obj);
            var arr = []
            if (str) {
                arr = JSON.parse(str)
                var ele = arr.find(item => item.productCode === productCode)
                if (ele) {
                    ele.num += 1;
                    localStorage.setItem('data', JSON.stringify(arr))
                    return false
                } else {
                    console.log(obj);
                    console.log(obj.score);
                    // var stock = this.parentElement.parentElement.lastElementChild.getAttribute('data-stock')
                    // console.log(stock);
                    if (score >= 1) {
                        obj.num = 1
                    }else{
                        alert('商商品库存没有了')
                        obj.num = 0
                        
                    }
                    
                }
            }
            arr.push(obj)
            console.log(arr);
            // 将新得到的数组数据存入本地存储
            localStorage.setItem('data', JSON.stringify(arr))
            alert('加入成功')
        } else {
            alert('请先登录')
        }
    }

}




